package cn.rengy.isql;

import lombok.Getter;
import lombok.Setter;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.List;
/**
 * 具名参数
 *
 */
@Getter
@Setter
public class NamedParameterHolder  implements ParameterHolder {

	
	private final String parameterName;

	private final int startIndex;

	private final int endIndex;

	public NamedParameterHolder(String parameterName, int startIndex, int endIndex) {
		this.parameterName = parameterName;
		this.startIndex = startIndex;
		this.endIndex = endIndex;
	}

	@Override
	public String parse(SqlParameterSource paramSource) {
		if (paramSource != null && paramSource.hasValue(parameterName)) {
			Object value = paramSource.getValue(parameterName);
			StringBuilder sb=new StringBuilder();
			SqlParser.appendParameters(sb, value);
			return sb.toString();
		} else {
			return "?";
		}
	}

	@Override
	public void appendValueList(List<Object> valueList, SqlParameterSource paramSource) {
		
		SqlParser.appendValueArray(valueList,paramSource.getValue(parameterName));
		
	}

}